package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.StudentRefund;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.IStudentRefundService;
import com.yuyou.fn.educational.service.IStudentRegService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@EventHandler(tables = "yyfn.course_class")
@Component
public class CourseClassChangeUpdater implements IEventHandler{




    @Resource
    private IStudentRefundService studentRefundService;

    @Resource
    private IStudentRegService studentRegService;





    @Override
    public void insert(String tableName, JSONArray rowList) {

    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        if (updateColumns.contains("teacher_id")){
            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                CourseClass courseClass = row.toJavaObject(CourseClass.class);

                if(courseClass.getTeacherId() != null ){
                    Example<StudentReg> example= StudentReg.createExample();
                    StudentReg studentReg =new StudentReg();
                    studentReg.setUpdateTime(TimeUtils.now());
                    example.createCriteria().andEqualTo("classId",courseClass.getClassId());
                    studentRegService.updateSelectiveByExample(studentReg,example);

                }

            }
        }


        if(updateColumns.contains("class_name")){

            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                Long classId = row.getLong("class_id");
                String className = row.getString("class_name");


                Example<StudentRefund> studentRefundExample = StudentRefund.createExample();
                studentRefundExample.createCriteria().andEqualTo("classId", classId );

                StudentRefund  updateStudentRefund = new StudentRefund();
                updateStudentRefund.setClassName(className);
                studentRefundService.updateSelectiveByExample(updateStudentRefund,studentRefundExample);

                Example<StudentReg> studentRegExample = StudentReg.createExample();
                studentRegExample.createCriteria().andEqualTo("classId", classId);

                StudentReg updateStudent = new StudentReg();
                updateStudent.setClassName(className);
                updateStudent.setUpdateTime(TimeUtils.now());
                studentRegService.updateSelectiveByExample(updateStudent, studentRegExample);
            }
        }

        if(updateColumns.contains("room_id")){
            for (int i = 0; i < rowList.size(); i++) {
                JSONObject row = rowList.getJSONObject(i);
                CourseClass courseClass = row.toJavaObject(CourseClass.class);
                Example<StudentReg> example= StudentReg.createExample();
                StudentReg studentReg =new StudentReg();
                studentReg.setUpdateTime(TimeUtils.now());
                example.createCriteria().andEqualTo("classId",courseClass.getClassId());
                studentRegService.updateSelectiveByExample(studentReg,example);
            }
        }






    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }
}
